package com.ddwl.gate.controller;


import com.ddwl.common.annotation.Auth;
import com.ddwl.common.constant.BaseEnum;
import com.ddwl.common.controller.BaseController;
import com.ddwl.gate.service.AppService;
import com.ddwl.schema.bo.base.PageBo;
import com.ddwl.schema.bo.gate.AppReleaseBo;
import com.ddwl.schema.vo.base.PageVo;
import com.ddwl.schema.vo.base.RestVo;
import com.ddwl.schema.vo.gate.AppReleaseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @author Fu Zhaohui
 * @date 2017/1/3
 */
@Api(tags = "App服务")
@RequestMapping("/app")
@RestController
@RequiredArgsConstructor
public class AppController extends BaseController {

    private final AppService appService;

    @PostMapping("/release")
    @Auth(required = false)
    @ApiOperation("获取App版本版本")
    public RestVo<AppReleaseVo> release(@RequestBody AppReleaseBo bo) {
        bo.setProduct(getProduct());
        bo.setPlatform(getPlatform());
        return RestVo.ok(appService.getAppVersion(bo));
    }

    @PostMapping("/create")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    @ApiOperation("发布新的版本")
    public RestVo<String> create(@RequestBody AppReleaseBo bo) {
        return RestVo.ok(appService.create(bo));
    }

    @PostMapping("/update")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    @ApiOperation("发布新的版本")
    public RestVo<AppReleaseVo> update(@RequestBody AppReleaseBo bo) {
        appService.update(bo);
        return RestVo.ok();
    }

    @PostMapping("/page")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    @ApiOperation("获取所有版本")
    public RestVo<PageVo<AppReleaseVo>> page(@Valid @RequestBody PageBo<AppReleaseBo> pageBo) {
        return RestVo.ok(appService.page(pageBo));
    }

    @DeleteMapping("/delete")
    @Auth(product = BaseEnum.ProductEnum.CONSOLE)
    @ApiOperation("删除版本")
    public RestVo delete(@RequestParam String id) {
        appService.delete(id);
        return RestVo.ok();
    }
}
